package com.frogs.ucenter.service.impl;

import java.util.Arrays;
import java.util.Date;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.util.date.DateUtil;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.OrderService;

@Service("orderService")
public class OrderServiceImpl extends AbsService implements OrderService {
	
	private static Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);

	@Override
	public JSONObject getOrderEveryStatusList(Integer pageNo, Integer pageSize,Integer userId,Integer status,String produceSource,Integer produceSourceType) throws Exception {
		// TODO Auto-generated method stub
		ApiUrl apiUrl = this.buildMallApiBaseUrl("MALL_ORDER_EVERY_STATUS_LIST");
		String[] otherParams = {ApiUrlParamEnum.pageNo.getParamName(),pageNo.toString(),
								ApiUrlParamEnum.pageSize.getParamName(),pageSize.toString(),
								ApiUrlParamEnum.userId.getParamName(),userId.toString(),
								ApiUrlParamEnum.applicablePlatformId.getParamName(),ApplicablePlatformEnum.WEB.getId()+"",
								
								ApiUrlParamEnum.status.getParamName(),status+"",
								ApiUrlParamEnum.produceSource.getParamName(),produceSource,
								ApiUrlParamEnum.produceSourceType.getParamName(),produceSourceType +""
								};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[getOrderEveryStatusList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("获取订单接口异常！");
		}
		return result;
	}
	
	@Override
	public JSONObject getOrderList(Integer pageNo, Integer pageSize,Integer userId) throws Exception {
		// TODO Auto-generated method stub
		ApiUrl apiUrl = this.buildMallApiBaseUrl("MALL_ORDER_LIST");
		String[] otherParams = {ApiUrlParamEnum.pageNo.getParamName(),pageNo.toString(),
								ApiUrlParamEnum.pageSize.getParamName(),pageSize.toString(),
								ApiUrlParamEnum.userId.getParamName(),userId.toString(),
								ApiUrlParamEnum.applicablePlatformId.getParamName(),ApplicablePlatformEnum.WEB.getId()+"",
								
								};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[getOrderList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("获取订单接口异常！");
		}
		return result;
	}

	@Override
	public JSONObject deleteOrder(Integer userId, Integer orderId) throws Exception {
		// TODO Auto-generated method stub
		ApiUrl apiUrl = this.buildMallApiBaseUrl("MALL_ORDER_DELETE");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(),userId.toString(),
								ApiUrlParamEnum.orderId.getParamName(),orderId.toString(),
								};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[deleteOrder] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch (Exception e) {
			throw new Exception("删除订单接口异常！");
		}
		return result;
	}

	@Override
	public JSONObject showOrder(Integer userId, Integer orderId) throws Exception {
		// TODO Auto-generated method stub
		ApiUrl apiUrl = this.buildMallApiBaseUrl("MALL_ORDER_SHOW");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(),userId.toString(),
				ApiUrlParamEnum.orderId.getParamName(),orderId.toString(),
				};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[showOrder] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch (Exception e) {
			throw new Exception("显示订单接口异常！");
		}
		return result;
	}

	@Override
	public JSONObject bindingOrderPhone(String phones, Integer userId) throws Exception {
		ApiUrl apiUrl = this.buildMallApiBaseUrl("MALL_ORDER_PHONE_BINDING");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(),userId.toString(),
				ApiUrlParamEnum.orderPhone.getParamName(), phones,
				};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[bindingOrderPhone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch (Exception e) {
			throw new Exception("绑定手机号码接口异常！");
		}
		return result;
	}
	
	@Override
	public JSONObject unBindingOrderPhone(String phones, Integer userId) throws Exception {
		ApiUrl apiUrl = this.buildMallApiBaseUrl("MALL_ORDER_PHONE_UNBINDING");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(),userId.toString(),
				ApiUrlParamEnum.orderPhone.getParamName(), phones,
				};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[bindingOrderPhone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch (Exception e) {
			throw new Exception("解绑手机号码接口异常！");
		}
		return result;
	}
	
	@Override
	public JSONObject syncOrders(String phones, Integer userId)
			throws Exception {
		ApiUrl apiUrl = this.buildMallApiBaseUrl("MALL_ORDER_SYNC");
		String endTime = DateUtil.getCurrentTime("yyyy-MM-dd HH:mm:ss");
		String startTime = DateUtil.formatDate(DateUtil.getDateAfterDays(new Date(),-90),"yyyy-MM-dd HH:mm:ss");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(),userId.toString(),
				ApiUrlParamEnum.orderPhone.getParamName(), phones,
				
				ApiUrlParamEnum.endTime.getParamName(), endTime,
				ApiUrlParamEnum.startTime.getParamName(), startTime};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[bindingOrderPhone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch (Exception e) {
			throw new Exception("解绑手机号码接口异常！");
		}
		return result;
	}
}
